package com.real8.romemp.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.real8.romemp.client.GameMessages;
import com.real8.romemp.client.place.HireAuxiliaPlace.EMenuSelection;

public class HireAuxiliaViewImpl extends Composite implements HireAuxiliaView {

	private static HireAuxiliaViewImplUiBinder uiBinder = GWT
			.create(HireAuxiliaViewImplUiBinder.class);

	interface HireAuxiliaViewImplUiBinder extends
			UiBinder<Widget, HireAuxiliaViewImpl> {
	}

	private Presenter presenter;

	@UiField
	HTMLPanel menuPanel;
	@UiField
	SpanElement hireSpan;
	@UiField
	SpanElement fireSpan;

	@UiField
	QuestionAndAnswerView qaView;

	@UiField
	HTMLPanel riotWarningPanel;

	@UiField
	HTMLPanel noRiotMessagePanel;

	@UiField
	HTMLPanel riotMessagePanel;

	@UiField
	HTMLPanel riotLosesPanel;
	@UiField
	SpanElement lootedGoldSpan;
	@UiField
	SpanElement killedPopulationSpan;
	@UiField
	SpanElement killedArtisansSpan;
	@UiField
	SpanElement killedShipBuildersSpan;

	@UiField
	Button continueButton;

	public HireAuxiliaViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void setPresenter(Presenter aPresenter) {
		this.presenter = aPresenter;

		qaView.setPresenter(new QuestionAndAnswerView.Presenter() {
			@Override
			public void onAnswer(int answer) {
				presenter.onAnswer(answer);
			}
		});
	}

	@Override
	public void hideAll() {
		menuPanel.setVisible(false);
		qaView.setVisible(false);
		riotWarningPanel.setVisible(false);
		noRiotMessagePanel.setVisible(false);
		riotMessagePanel.setVisible(false);
		riotLosesPanel.setVisible(false);
		continueButton.setVisible(false);
	}

	@Override
	public void showMenu(int availableAuxilia, int hiredAuxilia) {
		hireSpan.setInnerText("" + availableAuxilia);
		fireSpan.setInnerText("" + hiredAuxilia);

		menuPanel.setVisible(true);
	}

	@UiHandler("hireButton")
	void onHireButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onSelected(EMenuSelection.HIRE);
		}
	}

	@UiHandler("fireButton")
	void onFireButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onSelected(EMenuSelection.FIRE);
		}
	}

	@UiHandler("doNothingButton")
	void onDoNothingButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onSelected(EMenuSelection.DO_NOTHING);
		}
	}

	@Override
	public void showQuestion(EMenuSelection selection) {
		GameMessages messages = (GameMessages) GWT.create(GameMessages.class);

		switch (selection) {
		case HIRE:
			qaView.showQuestion(messages.toHireAuxilia());
			break;
		case FIRE:
			qaView.showQuestion(messages.toFireAuxilia());
			break;
		}
	}

	@Override
	public void showRiotWarning() {
		riotWarningPanel.setVisible(true);
	}

	@Override
	public void showNoRiotMessage() {
		noRiotMessagePanel.setVisible(true);
	}

	@Override
	public void showRiotMessage() {
		riotMessagePanel.setVisible(true);
	}

	@Override
	public void showRiotLoses(int lootedGold, int killedPopulation,
			int killedArtisans, int killedShipBuilders) {
		lootedGoldSpan.setInnerText("" + lootedGold);
		killedPopulationSpan.setInnerText("" + killedPopulation);
		killedArtisansSpan.setInnerText("" + killedArtisans);
		killedShipBuildersSpan.setInnerText("" + killedShipBuilders);

		riotLosesPanel.setVisible(true);
	}

	@Override
	public void showContinue() {
		continueButton.setVisible(true);
	}

	@UiHandler("continueButton")
	void onContinueButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onContinue();
		}
	}
}
